package uranium.gui;

import java.awt.BorderLayout;
import java.awt.Toolkit;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;

//import org.jfree.base.log.PadMessage;

import uranium.db.DBManager;
import uranium.db.GestionePassword;
import java.awt.Rectangle;

/**
 * @author hunterbit
 *
 */
public class UtenteGUI extends JDialog
{

	private JPanel	jContentPane	= null;
	private JLabel lblUserID = null;
	private JTextField txtUserID = null;
	private JLabel lblPassword = null;
	private JPasswordField txtPsw = null;
	private JButton btnInserisci = null;
	
	private DBManager dbm=null;
	private GestionePassword gp=null;
	private String vUtente=null;  //  @jve:decl-index=0:
	private JLabel lblRipPasw = null;
	private JPasswordField txtPsw2 = null;
	private JFrame padre=null;
	private JButton jButton = null;
	/**
	 * This is the default constructor
	 */
	public UtenteGUI(DBManager dbm,JFrame padre)
	{
		super(padre,true);
		this.gp=new GestionePassword(dbm);
		this.dbm=dbm;
		this.padre=padre;
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize()
	{
		this.setSize(331, 257);
		this.setTitle("Gestione Utenti"); 
		this.setContentPane(getJContentPane());
		String dati[]=gp.getUtenteAdmin();
		/*if(dati!=null)
		{
			txtUserID.setText(dati[0]);
			txtPsw.setText(dati[1]);
			vUtente=dati[0];
		}*/
		centro();
	}
	private void centro()
	{
		int x = (int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth() - this.getWidth()) / 2;
		int y = (int) (Toolkit.getDefaultToolkit().getScreenSize().getHeight() - this.getHeight()) / 2;
		this.setLocation(x, y);
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane()
	{
		if (jContentPane == null)
		{
			lblRipPasw = new JLabel();
			lblRipPasw.setBounds(new java.awt.Rectangle(15,150,126,21));  // Generated
			lblRipPasw.setText("Ripeti Password");  // Generated
			lblPassword = new JLabel();
			lblPassword.setBounds(new java.awt.Rectangle(15,95,126,21));  // Generated
			lblPassword.setText("Password");  // Generated
			lblUserID = new JLabel();
			lblUserID.setBounds(new java.awt.Rectangle(15,40,126,21));  // Generated
			lblUserID.setText("User ID");  // Generated
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED), "Inserimento Password", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null));  // Generated
			jContentPane.add(lblUserID, null);  // Generated
			jContentPane.add(getTxtUserID(), null);  // Generated
			jContentPane.add(lblPassword, null);  // Generated
			jContentPane.add(getTxtPsw(), null);  // Generated
			jContentPane.add(getBtnInserisci(), null);  // Generated
			jContentPane.add(lblRipPasw, null);  // Generated
			jContentPane.add(getTxtPsw2(), null);  // Generated
			jContentPane.add(getJButton(), null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes txtUserID	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getTxtUserID()
	{
		if (txtUserID == null)
		{
			try
			{
				txtUserID = new JTextField();
				txtUserID.setBounds(new java.awt.Rectangle(15,65,161,21));  // Generated
			} catch (java.lang.Throwable e)
			{
				// TODO: Something
			}
		}
		return txtUserID;
	}

	/**
	 * This method initializes txtPsw	
	 * 	
	 * @return javax.swing.JPasswordField	
	 */
	private JPasswordField getTxtPsw()
	{
		if (txtPsw == null)
		{
			try
			{
				txtPsw = new JPasswordField();
				txtPsw.setBounds(new java.awt.Rectangle(15,120,161,21));  // Generated
			} catch (java.lang.Throwable e)
			{
				// TODO: Something
			}
		}
		return txtPsw;
	}

	/**
	 * This method initializes btnInserisci	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnInserisci()
	{
		if (btnInserisci == null)
		{
			try
			{
				btnInserisci = new JButton();
				btnInserisci.setBounds(new java.awt.Rectangle(200,40,101,26));  // Generated
				btnInserisci.setText("Inserisci");  
				btnInserisci.addActionListener(new java.awt.event.ActionListener()
				{
					public void actionPerformed(java.awt.event.ActionEvent e)
					{
						inserisci();
					}
				});
			} catch (java.lang.Throwable e)
			{
				// TODO: Something
			}
		}
		return btnInserisci;
	}

	/**
	 * 
	 */
	private void inserisci(){
		Statement st=dbm.getNewStatement();
		if(gp.utenteEsiste(txtUserID.getText())){
			JOptionPane.showMessageDialog(this, "Utente gia' presente, scegliere nuova user ID", "ERRORE UTENTE", JOptionPane.ERROR_MESSAGE);
			/*boolean uguali=gp.passwordUguali(txtPsw.getPassword(), txtPsw2.getPassword());
			if(!uguali)
			{
				JOptionPane.showMessageDialog(this, "Le due password devono \nessere uguali", "ERRORE INSERIMENTO PASSWORD", JOptionPane.ERROR_MESSAGE);
				return;
			}
			String pass=new String(txtPsw.getPassword());
			String update="UPDATE utenti SET password='"+pass+"' WHERE username='"+txtUserID.getText()+"'";
			try
			{
				st.executeUpdate(update);
			} catch (SQLException e)
			{
				e.printStackTrace();
			}finally{
				if(st!=null)
				{
					try
					{
						st.close();
					} catch (SQLException e)
					{
						e.printStackTrace();
					}
				}
			}*/
		}else{
			boolean uguali=gp.passwordUguali(txtPsw.getPassword(), txtPsw2.getPassword());
			if(!uguali)
			{
				JOptionPane.showMessageDialog(this, "Le due password devono \nessere uguali", "ERRORE INSERIMENTO PASSWORD", JOptionPane.ERROR_MESSAGE);
				return;
			}
			String pass=new String(txtPsw.getPassword());
			String insert="INSERT INTO utenti (username,password) VALUES('"+txtUserID.getText()+"','"+pass+"')";
			try
			{
				st.executeUpdate(insert);
			} catch (SQLException e)
			{
				e.printStackTrace();
			}finally{
				if(st!=null)
				{
					try
					{
						st.close();
					} catch (SQLException e)
					{
						e.printStackTrace();
					}
				}
			}
			this.dispose();	
		}
	}

	/**
	 * This method initializes txtPsw2	
	 * 	
	 * @return javax.swing.JPasswordField	
	 */
	private JPasswordField getTxtPsw2()
	{
		if (txtPsw2 == null)
		{
			try
			{
				txtPsw2 = new JPasswordField();
				txtPsw2.setBounds(new java.awt.Rectangle(15,175,161,21));  // Generated
			} catch (java.lang.Throwable e)
			{
				// TODO: Something
			}
		}
		return txtPsw2;
	}

	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.setBounds(new Rectangle(200, 88, 101, 26));
			jButton.setText("Annulla");
			jButton.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					close();
				}
			});
		}
		return jButton;
	}
	
	private void close(){
		this.dispose();
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
